<template>
  <el-upload
    class="avatar-uploader"
    :action="'api'+API.FILE_UPLOAD"
    :before-upload="beforeAvatarUpload"
    :on-preview="handlePictureCardPreview"
    accept=".jpg,.png"
    list-type="picture-card"
    :file-list="list"
    :on-success="successHandle"
    :on-change="changeHandle"
  >
    <el-icon
      class="avatar-uploader-icon"
    >
      <Plus />
      <span class="el-upload__tip">
        支持jpg、png格式图片
      </span>
    </el-icon>
  </el-upload>
  <NormalDialog ref="dialog$1">
    <img
      w-full
      :src="dialogImageUrl"
      alt="Preview Image"
    >
  </NormalDialog>
</template>
  
<script lang="ts" setup>
import { ref,watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import API from '@api/api'
import NormalDialog from '@c/Dialog/NormalDialog.vue'
const emits =defineEmits(['update:modelValue'])
const props = defineProps({
    modelValue:{
        type:Array,
        default:()=>[]
    },
    limit:{
        type:[Number,String],
        default:1

    },
    accept:{
        type:String,
        default:'.pdf'

    }
  
})

const dialog$1 = ref(null)
const list = ref(props.modelValue)
const dialogImageUrl = ref('')
  
const beforeAvatarUpload= (rawFile) => {
    const {type,size} = rawFile
    if (!['image/jpeg','image/png','image.jpg'].includes(type) ) {
        ElMessage.error('文件格式不允许')
        return false
    } else if (size / 1024 / 1024 > 5) {
        ElMessage.error('文件大小不能大于2M')
        return false
    }
    return true
}

const handlePictureCardPreview = (uploadFile) => {
    dialogImageUrl.value = uploadFile.url
    dialog$1.value.visible=true
}

const successHandle=(resp,file)=>{
    Object.assign(file,resp.data)
}

const changeHandle=(file,fileList)=>{
    if(file.status==='success'){
        const emitInfo = fileList.map(f=>{
            const {groupId,name,docId}=f
            return{groupId,name,docId}
        })
        emits('update:modelValue',emitInfo)
    }

}


</script>
  
  <style scoped>
  .avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  </style>
  
  <style>
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }
  
  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }
  
  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    flex-direction:column
  }
  </style>